package com.lg.dao;

import java.util.ArrayList;
import java.util.List;
@SuppressWarnings("unchecked")
public class Example<T extends Criteria> {
	
	protected String orderByClause;
	
	protected List<Criteria> oredCriteria;

	
	protected Class<T> clazz;
	
	
	public Example(Class clazz) {
		oredCriteria = new ArrayList();
		this.clazz=clazz;
	}

	
	public Example(Example example){
		oredCriteria=example.oredCriteria;
		clazz=example.clazz;
	}

	
	public void setOrderByClause(String orderByClause) {
		this.orderByClause = orderByClause;
	}

	
	public String getOrderByClause() {
		return orderByClause;
	}

	
	public List<Criteria> getOredCriteria() {
		return oredCriteria;
	}

	public void or(Criteria criteria) {
		oredCriteria.add(criteria);
	}

	
	public T createCriteria() {
		Criteria criteria = createCriteriaInternal();
		if (oredCriteria.size() == 0) {
			oredCriteria.add(criteria);
		}
		return (T)criteria;
	}

	
	protected Criteria createCriteriaInternal() {
		Criteria criteria=null;
		try {
			criteria = (Criteria)clazz.newInstance();
			oredCriteria.add(criteria);
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		return criteria;
	}

	
	public void clear() {
		oredCriteria.clear();
	}
}
